import 'package:flutter/material.dart';
import 'package:shenling_flutter/utils/auth.dart';
import 'package:shenling_flutter/utils/toastMsg.dart';

class Settingview extends StatefulWidget {
  const Settingview({super.key});

  @override
  State<Settingview> createState() => _SettingviewState();
}

class _SettingviewState extends State<Settingview> {
  //列表信息
  List settingInfo = [
    {'title': '换绑手机', 'icon': Icons.chevron_right},
    {'title': '修改密码', 'icon': Icons.chevron_right},
    {'title': '消息通知设置', 'icon': Icons.chevron_right},
    {'title': '关于应用', 'icon': Icons.chevron_right},
    {'title': '清理缓存', 'icon': Icons.chevron_right},
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('系统设置'),
          backgroundColor: Colors.white,
          centerTitle: true,
        ),
        body: Container(
            margin: const EdgeInsets.only(top: 20),
            child: Column(
              children: [
                ListView.separated(
                  shrinkWrap: true,
                  itemCount: settingInfo.length,
                  separatorBuilder: (BuildContext context, int index) {
                    return const Divider(
                      height: 1,
                      color: Color.fromARGB(255, 193, 193, 193),
                    );
                  },
                  itemBuilder: (BuildContext context, int index) {
                    return Container(
                      color: Colors.white,
                      padding: const EdgeInsets.all(15),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(settingInfo[index]['title']),
                          Icon(
                            settingInfo[index]['icon'],
                          )
                        ],
                      ),
                    );
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                //退出登录按钮
                SizedBox(
                  width: MediaQuery.of(context).size.width - 50,
                  child: TextButton(
                    style: TextButton.styleFrom(
                        backgroundColor: Colors.white,
                        padding: const EdgeInsets.all(20),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(5),
                        )),
                    onPressed: () async {
                      if (Auth().getToken() == null) {
                        Toastmsg.showToast('您还未登录,请先登录后再操作');
                        return;
                      }
                      //二次确认弹窗
                      showDialog(
                          context: context,
                          builder: (context) {
                            return AlertDialog(
                                title: const Text('温馨提示'),
                                content: const Text('确定要退出登录吗？'),
                                actions: [
                                  TextButton(
                                    child: const Text('取消'),
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                  ),
                                  TextButton(
                                      child: const Text('确定'),
                                      onPressed: () async {
                                        Auth().getToken();
                                        await Auth().removeToken();
                                        Navigator.pop(context);
                                        // 返回登录页->清空页面栈
                                        Navigator.pushNamedAndRemoveUntil(
                                            context,
                                            '/login',
                                            (route) => false);
                                        Toastmsg.showSuccess('退出登录成功');

                                        // Navigator.popUntil(context, (router) {
                                        //   return router.settings.name == '/login';
                                        // });
                                      })
                                ]);
                          });
                    },
                    child: const Text(
                      '退出登录',
                      style: TextStyle(color: Colors.black),
                    ),
                  ),
                )
              ],
            )));
  }
}
